package com.wcs.spring.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.wcs.spring.entity.User;
import com.wcs.spring.mapper.UserMapper;
import com.wcs.spring.service.UserService;
import com.wcs.spring.util.Md5;
import com.wcs.spring.util.PageUtil;
import com.wcs.spring.util.WeekDateUtil;

/**
 * <p>
 * Title: Phicomm_[子系统统名]_[模块名]
 * </p>
 * <p>
 * Description: [描述该类概要功能介绍]
 * </p>
 * 
 * @author hexuan2014
 * @version $Revision$ 2015-6-15
 * @author (lastest modification by $Author$)
 * @since 1.0
 */
@Repository(value = "userService")
public class UserServiceImpl implements UserService {

	@Resource(name = "userMapper")
	private UserMapper userMapper;

	@Override
	public List<User> queryUserList(User user, PageUtil pageUtil) {
		StringBuilder sql = new StringBuilder();
		if ("xz".equals(user.getUserType())) {
			sql.append(" select u.*,s.schoolName schoolIdName from user u,school s where u.flag=0 and u.schoolId=s.id ");
			if (StringUtils.isNotBlank(user.getUserName())) {
				sql.append(" and u.userName like '%" + user.getUserName() + "%' ");
			}
			if (StringUtils.isNotBlank(user.getChineseName())) {
				sql.append(" and u.chineseName like '%" + user.getChineseName() + "%' ");
			}
			if (StringUtils.isNotBlank(user.getEnglishName())) {
				sql.append(" and u.englishName like '%" + user.getEnglishName() + "%' ");
			}
			if (StringUtils.isNotBlank(user.getUserType())) {
				sql.append(" and u.userType='" + user.getUserType() + "' ");
			}

			if (StringUtils.isNotBlank(pageUtil.getOrderColunm()) && StringUtils.isNotBlank(pageUtil.getOrderMode())) {
				sql.append(" order by u." + pageUtil.getOrderColunm() + " " + pageUtil.getOrderMode());
			}
			else {
				sql.append(" order by u.updateTime desc ");
			}
		}
		else {
			sql.append(" select * from user u where flag=0 ");
			if (StringUtils.isNotBlank(user.getUserName())) {
				sql.append(" and u.userName like '%" + user.getUserName() + "%' ");
			}
			if (StringUtils.isNotBlank(user.getChineseName())) {
				sql.append(" and u.chineseName like '%" + user.getChineseName() + "%' ");
			}
			if (StringUtils.isNotBlank(user.getEnglishName())) {
				sql.append(" and u.englishName like '%" + user.getEnglishName() + "%' ");
			}
			if (StringUtils.isNotBlank(user.getUserType())) {
				sql.append(" and u.userType='" + user.getUserType() + "' ");
			}
			if (null != user.getSchoolId()) {
				sql.append(" and u.schoolId=" + user.getSchoolId() + " ");
			}
			if (StringUtils.isNotBlank(pageUtil.getOrderColunm()) && StringUtils.isNotBlank(pageUtil.getOrderMode())) {
				sql.append(" order by u." + pageUtil.getOrderColunm() + " " + pageUtil.getOrderMode());
			}
			else {
				sql.append(" order by u.updateTime desc ");
			}
		}

		return userMapper.queryUserList(sql.toString());
	}

	@Override
	public List<User> queryUserByType(User user) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select id,userName from user u where flag=0 ");
		if (StringUtils.isNotBlank(user.getUserType())) {
			sql.append(" and u.userType='" + user.getUserType() + "' ");
		}
		if (null != user.getSchoolId()) {
			sql.append(" and u.schoolId=" + user.getSchoolId() + " ");
		}
		sql.append(" order by u.userName asc ");
		return userMapper.queryUserList(sql.toString());
	}

	@Override
	public User queryUserByParam(User user) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select u.*,s.schoolName schoolIdName  from user u ,school s  where u.schoolId=s.id and u.flag=0 ");
		if (StringUtils.isNotBlank(user.getUserName())) {
			sql.append(" and u.userName='" + user.getUserName() + "' ");
		}
		if (StringUtils.isNotBlank(user.getPassword())) {
			sql.append(" and u.password='" + Md5.MD5(user.getPassword()) + "' ");
		}
		if (null != user.getSchoolId()) {
			sql.append(" and u.schoolId=" + user.getSchoolId() + " ");
		}
		return userMapper.queryUserByParam(sql.toString());
	}

	@Override
	public int addUser(User user) {
		// TODO Auto-generated method stub
		// String sql =
		// "CREATE TABLE IF NOT EXISTS user_" + user.getUserName() + " (" + " id int(11) NOT NULL AUTO_INCREMENT,"
		// + " userName varchar(55) DEFAULT NULL," + " birthday date DEFAULT NULL,"
		// + " PRIMARY KEY (id)) ENGINE=MyISAM DEFAULT CHARSET=utf8";
		user.setPassword(Md5.MD5(user.getUserName()));
		return userMapper.addUser(user);
	}

	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		String updateTimeStr = WeekDateUtil.dateToString(user.getUpdateTime(), "yyyy-MM-dd HH:mm:ss");
		StringBuilder sql = new StringBuilder();
		sql.append("update user u set u.updateTime='" + updateTimeStr + "',u.updateUser='" + user.getUpdateUser() + "'");
		if (StringUtils.isNotBlank(user.getUserName())) {
			sql.append(",u.userName='" + user.getUserName() + "'");
		}
		if ("xz".equals(user.getUserType()) && null != user.getSchoolId() && user.getSchoolId() > 0) {
			sql.append(",u.schoolId=" + user.getSchoolId());
		}
		if (StringUtils.isNotBlank(user.getPassword())) {
			sql.append(",u.password='" + Md5.MD5(user.getPassword()) + "'");
		}
		if (StringUtils.isNotBlank(user.getAddress())) {
			sql.append(",u.address='" + user.getAddress() + "'");
		}
		if (StringUtils.isNotBlank(user.getChineseName())) {
			sql.append(",u.chineseName='" + user.getChineseName() + "'");
		}
		if (StringUtils.isNotBlank(user.getEnglishName())) {
			sql.append(",u.englishName='" + user.getEnglishName() + "'");
		}
		if (StringUtils.isNotBlank(user.getMajorName())) {
			sql.append(",u.majorName='" + user.getMajorName() + "'");
		}
		if (StringUtils.isNotBlank(user.getSchoolName())) {
			sql.append(",u.schoolName='" + user.getSchoolName() + "'");
		}
		if (StringUtils.isNotBlank(user.getMobile())) {
			sql.append(",u.mobile='" + user.getMobile() + "'");
		}
		if (StringUtils.isNotBlank(user.getSex())) {
			sql.append(",u.sex='" + user.getSex() + "'");
		}
		if (StringUtils.isNotBlank(user.getTelNo())) {
			sql.append(",u.telNo='" + user.getTelNo() + "'");
		}
		if (null != user.getAge() && user.getAge() > 0) {
			sql.append(",u.age=" + user.getAge());
		}
		if (null != user.getStudentLevel()) {
			sql.append(",u.studentLevel=" + user.getStudentLevel());
		}
		if (null != user.getOnlineStartDate()) {
			sql.append(",u.onlineStartDate='" + user.getOnlineStartDate() + "'");
		}
		if (null != user.getOnlineEndDate()) {
			sql.append(",u.onlineEndDate='" + user.getOnlineEndDate() + "'");
		}
		if (null != user.getOfflineStartDate()) {
			sql.append(",u.offlineStartDate='" + user.getOfflineStartDate() + "'");
		}
		if (null != user.getOfflineEndDate()) {
			sql.append(",u.offlineEndDate='" + user.getOfflineEndDate() + "'");
		}
		sql.append(" where u.id=" + user.getId());
		userMapper.updateUser(sql.toString());
	}

	@Override
	public void delUser(User user) {
		// TODO Auto-generated method stub
		userMapper.delUser(user);
	}

	@Override
	public User queryUserById(Integer id) {
		// TODO Auto-generated method stub
		return userMapper.queryUserById(id);
	}

}
